﻿using System;
using System.Linq.Expressions;
using System.Runtime.CompilerServices;
using Autodesk.Revit.DB;
using Microsoft.CSharp.RuntimeBinder;

namespace YJKRevitCode.TransactionUtility
{
	public class RevitTransactionManger
	{
		public static bool DoTransaction(Document inputDoc, TransactionWorkDelegate useDelegate, object delegateUseValue, out Exception faceException, bool ifIsSubTransaction = false, string strUseTransactionName = null)
		{
			object arg = null;
			faceException = null;
			if (strUseTransactionName == null)
			{
				strUseTransactionName = RevitTransactionManger.m_strDefultTransactionName;
			}
			if (ifIsSubTransaction)
			{
				arg = new SubTransaction(inputDoc);
			}
			else
			{
				arg = new Transaction(inputDoc, strUseTransactionName);
			}
			bool result = false;
			try
			{
                //if (RevitTransactionManger.<DoTransaction>o__SiteContainer0.<>p__Site1 == null)
                //{
                //    RevitTransactionManger.<DoTransaction>o__SiteContainer0.<>p__Site1 = CallSite<Action<CallSite, object>>.Create(Binder.InvokeMember(CSharpBinderFlags.ResultDiscarded, "Start", null, typeof(RevitTransactionManger), new CSharpArgumentInfo[]
                //    {
                //        CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null)
                //    }));
                //}
                //RevitTransactionManger.<DoTransaction>o__SiteContainer0.<>p__Site1.Target(RevitTransactionManger.<DoTransaction>o__SiteContainer0.<>p__Site1, arg);
                //bool flag = useDelegate(delegateUseValue);
                //if (RevitTransactionManger.<DoTransaction>o__SiteContainer0.<>p__Site2 == null)
                //{
                //    RevitTransactionManger.<DoTransaction>o__SiteContainer0.<>p__Site2 = CallSite<Action<CallSite, object>>.Create(Binder.InvokeMember(CSharpBinderFlags.ResultDiscarded, "Commit", null, typeof(RevitTransactionManger), new CSharpArgumentInfo[]
                //    {
                //        CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null)
                //    }));
                //}
                //RevitTransactionManger.<DoTransaction>o__SiteContainer0.<>p__Site2.Target(RevitTransactionManger.<DoTransaction>o__SiteContainer0.<>p__Site2, arg);
                //result = flag;
			}
			catch (Exception ex)
			{
				faceException = ex;
                //if (RevitTransactionManger.<DoTransaction>o__SiteContainer0.<>p__Site3 == null)
                //{
                //    RevitTransactionManger.<DoTransaction>o__SiteContainer0.<>p__Site3 = CallSite<Func<CallSite, object, bool>>.Create(Binder.UnaryOperation(CSharpBinderFlags.None, ExpressionType.IsTrue, typeof(RevitTransactionManger), new CSharpArgumentInfo[]
                //    {
                //        CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null)
                //    }));
                //}
                //Func<CallSite, object, bool> target = RevitTransactionManger.<DoTransaction>o__SiteContainer0.<>p__Site3.Target;
                //CallSite <>p__Site = RevitTransactionManger.<DoTransaction>o__SiteContainer0.<>p__Site3;
                //if (RevitTransactionManger.<DoTransaction>o__SiteContainer0.<>p__Site4 == null)
                //{
                //    RevitTransactionManger.<DoTransaction>o__SiteContainer0.<>p__Site4 = CallSite<Func<CallSite, object, object>>.Create(Binder.InvokeMember(CSharpBinderFlags.None, "HasStarted", null, typeof(RevitTransactionManger), new CSharpArgumentInfo[]
                //    {
                //        CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null)
                //    }));
                //}
                //if (target(<>p__Site, RevitTransactionManger.<DoTransaction>o__SiteContainer0.<>p__Site4.Target(RevitTransactionManger.<DoTransaction>o__SiteContainer0.<>p__Site4, arg)))
                //{
                //    if (RevitTransactionManger.<DoTransaction>o__SiteContainer0.<>p__Site5 == null)
                //    {
                //        RevitTransactionManger.<DoTransaction>o__SiteContainer0.<>p__Site5 = CallSite<Action<CallSite, object>>.Create(Binder.InvokeMember(CSharpBinderFlags.ResultDiscarded, "RollBack", null, typeof(RevitTransactionManger), new CSharpArgumentInfo[]
                //        {
                //            CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null)
                //        }));
                //    }
                //    RevitTransactionManger.<DoTransaction>o__SiteContainer0.<>p__Site5.Target(RevitTransactionManger.<DoTransaction>o__SiteContainer0.<>p__Site5, arg);
                //}
				result = false;
			}
			return result;
		}

		private static string m_strDefultTransactionName = "defultName";

        //[CompilerGenerated]
        //private static class <DoTransaction>o__SiteContainer0
        //{
        //    public static CallSite<Action<CallSite, object>> <>p__Site1;

        //    public static CallSite<Action<CallSite, object>> <>p__Site2;

        //    public static CallSite<Func<CallSite, object, bool>> <>p__Site3;

        //    public static CallSite<Func<CallSite, object, object>> <>p__Site4;

        //    public static CallSite<Action<CallSite, object>> <>p__Site5;
        //}
	}
}
